﻿#if NETFX_CORE
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
#else
using System.Windows;
#endif

namespace FreshMeat.Xaml
{
   /// <summary>
   /// Provides a way to set certain properties that either don't exist or work differently
   /// on different platforms while maintaining XMAL compatibility.
   /// </summary>
   public class Props
   {
      /// <summary>
      /// Allows the use of the WPF-only "UIElement.Focusable" property from XAML in 
      /// a way that is compatible with Silverlight, which has no such property.
      /// </summary>
      public static readonly DependencyProperty FocusableProperty =
         DependencyProperty.RegisterAttached("Focusable", typeof(bool), typeof(Props), new PropertyMetadata(true, (d, e) => FocusableChanged(d, e.NewValue)));

      static void FocusableChanged(DependencyObject dependencyObject, object newValue)
      {
#if !SILVERLIGHT && !NETFX_CORE
         var element = dependencyObject as UIElement;
         if (element != null)
            element.SetValue(UIElement.FocusableProperty, newValue);
#endif
      }

      public static bool GetFocusable(FrameworkElement element)
      {
         return (bool)element.GetValue(FocusableProperty);
      }

      public static void SetFocusable(FrameworkElement element, bool value)
      {
         element.SetValue(FocusableProperty, value);
      }

      /// <summary>
      /// Allows the use of the WPF+Silverlight "FrameworkElement.Cursor" property from XAML in 
      /// a way that is compatible with NETFX_CORE, which has no such property.
      /// </summary>
      public static readonly DependencyProperty CursorProperty =
         DependencyProperty.RegisterAttached("Cursor", typeof(object), typeof(Props), new PropertyMetadata(null, (d, e) => CursorChanged(d, e.NewValue)));

      static void CursorChanged(DependencyObject dependencyObject, object newValue)
      {
#if !NETFX_CORE
         var element = dependencyObject as FrameworkElement;
         if (element != null)
            element.SetValue(FrameworkElement.CursorProperty, newValue);
#endif
      }

      public static object GetCursor(FrameworkElement element)
      {
         return element.GetValue(CursorProperty);
      }

      public static void SetCursor(FrameworkElement element, object value)
      {
         element.SetValue(CursorProperty, value);
      }
   }
}
